package _general;

import java.awt.Graphics;
import java.util.Collection;
import java.util.Iterator;

import javax.swing.JPanel;

import _general.geometry.Edge;
import _general.geometry.Node;
import _general.performanceTest.IllegalConfigException;

class DrawPanel<E> extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4403222193555919406L;

	Collection<E> pointSet;
	
	private double scalingFactorX;
	private double scalingFactorY;
	
	public DrawPanel(Collection<E> pointSet) {
		this.pointSet = pointSet;
	}
	
  @Override
  protected void paintComponent( Graphics g ) throws RuntimeException {
	  //super.paintComponent( g );

	  try {
			this.setScalingFactor();
		} catch (IllegalConfigException e) { e.printStackTrace(); }
		
	  Iterator<E> it = (Iterator<E>) pointSet.iterator();
	  
	  while( it.hasNext()) {
		  E e = it.next();
		  
		  if(e.getClass().equals(Node.class))
			  g.drawOval(
				  (int) (((Node) e).getX() * scalingFactorX) - 5, 
				  (this.getSize().height - (int) (((Node) e).getY() * scalingFactorY) - 5), 
				  10, 10); 
		  
		
		  if (e.getClass().equals(Node.class) && ((Node)e).isStart()) {
			  Edge edge = ((Node)e).getEdge();

			  g.drawLine(
					  (int) (edge.getStart().getX() * scalingFactorX), 
					  (this.getSize().height - (int) ((double) edge.getStart().getY() * (double) scalingFactorY)),
					  (int) (edge.getEnd().getX() * scalingFactorX),
					  (this.getSize().height - (int) ((double)edge.getEnd().getY() * (double) scalingFactorY)));
		  }
		  
	  }
  }
  
  private void setScalingFactor() throws IllegalConfigException {
	  if (Config.problemRange.length < 2)
		  throw new IllegalConfigException();
	  
	  this.scalingFactorX = (double) this.getSize().width / (double) Config.problemRange[0];
	  this.scalingFactorY = (double) this.getSize().height / (double) Config.problemRange[1];
  }
}
